package com.lin.remote;

import com.lzl.constant.AmapConfigConstants;
import com.lzl.responese.DirectionResponese;
import lombok.extern.slf4j.Slf4j;
import net.sf.json.JSONArray;
import net.sf.json.JSONObject;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.http.ResponseEntity;
import org.springframework.stereotype.Service;
import org.springframework.web.client.RestTemplate;

/**
 * //TODO
 *
 * @author zhaolinlin
 * @description //TODO
 * @createTime 2022/8/18 22:19
 */
@Service
@Slf4j
public class MapDirectionClient {

    @Value("${amap.key}")
    private String amapKey;

    @Autowired
    private RestTemplate restTemplate;
//https://restapi.amap.com/v3/direction/driving?origin=&destination=&extensions=base&output=json&key=8d30fb1481b310324acccc7fd36bf5c1
    public DirectionResponese direction(String depLongitude,String depLatitude,String destLongitude,String destLatitude){
      //组装请求调用url
        StringBuilder urlBuilder = new StringBuilder();
        urlBuilder.append(AmapConfigConstants.DIRECTION_URL);
        urlBuilder.append("?");
        urlBuilder.append("origin=" + depLongitude + "," + depLatitude);
        urlBuilder.append("&");
        urlBuilder.append("destination="+destLongitude + "," + destLatitude);
        urlBuilder.append("&");
        urlBuilder.append("extensions=base");
        urlBuilder.append("&");
        urlBuilder.append("output=json");
        urlBuilder.append("&");
        urlBuilder.append("key="+ amapKey);
        log.info(urlBuilder.toString());

        ResponseEntity<String> forEntity = restTemplate.getForEntity(urlBuilder.toString(), String.class);
        String directionString = forEntity.getBody();
        log.info("高德地图,路径规划,返回信息");
        DirectionResponese directionResponese = parseDirection(directionString);

        return directionResponese;

    }

    public DirectionResponese parseDirection(String directionString){
        DirectionResponese driretionAndDuration= null;
            JSONObject result = JSONObject.fromObject(directionString);
        try {
            if (result.has(AmapConfigConstants.STATUS)) {
                int status = result.getInt(AmapConfigConstants.STATUS);
                if (status == 1) {
                    if (result.has(AmapConfigConstants.ROUTE)) {
                        JSONObject route = result.getJSONObject(AmapConfigConstants.ROUTE);
                        JSONArray paths = route.getJSONArray(AmapConfigConstants.PATHS);
                        JSONObject path = paths.getJSONObject(0);
                        driretionAndDuration = new DirectionResponese();

                        if (path.has(AmapConfigConstants.DISTANCE)) {
                            driretionAndDuration.setDirection(path.getInt(AmapConfigConstants.DISTANCE));
                        }

                        if (path.has(AmapConfigConstants.DURATION)) {
                            driretionAndDuration.setDuration(path.getInt(AmapConfigConstants.DURATION));
                        }

                    }
                }
            }
        }catch (Exception e){

        }
        return driretionAndDuration;
    }


}
